package user

import (
	service "cms/serivce"
	"encoding/base64"
	emodel "engine/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"lib/db"
	"lib/response"
	"net/http"
)

type ImHandler struct{}

//	用户登录
//
// @Summary		登录
// @Description	登录
// @Tags		cms/accounts
// @Accept		json
// @Produce		json
// @Param		RequestBody body model.LoginRequest true "请求参数"
// @Success		200 {object} response.Response[response.AuthResponse[model.SysUser, any]]
// @Router		/api/user/v2/token [get]
func (handler *ImHandler) Token(ctx *gin.Context) {
	operator, err := getOperator(ctx)
	if err != nil {
		response.Error(ctx, err)
		return
	}
	username, password := GetPwd(&operator.My_user)

	hxClient := service.NewHuanXinClient()
	hxClient.GetToken()
	loginResp, err := hxClient.UserLogin(username, password)
	if err != nil {
		response.Error(ctx, err)
		return
	}

	response.OkData(ctx, loginResp)
	return
}

// @Summary		新消息
// @Description	新消息
// @Tags		cms/accounts
// @Accept		json
// @Produce		json
// @Param		RequestBody body model.LoginRequest true "请求参数"
// @Success		200 {object} response.Response[response.AuthResponse[model.SysUser, any]]
// @Router		/api/user/v2/new-msg [get]

func (handler *ImHandler) NewMsg(ctx *gin.Context) {
	operator, err := getOperator(ctx)
	if err != nil {
		response.Error(ctx, err)
		return
	}
	logList, logListErr := service.GetNewMsgNew(ctx, operator.Id)
	if logListErr != nil {
		response.Error(ctx, logListErr)
		return
	}
	response.OkData(ctx, logList)
	return
}

func (handler *ImHandler) NewMsgKL(ctx *gin.Context) {
	//operator, err := getOperator(ctx)
	//if err != nil {
	//	response.Error(ctx, err)
	//	return
	//}
	cc := emodel.My_msg{}
	if err := db.GetDb().Order("id desc").First(&cc).Error; err != nil {
		response.Error(ctx, err)
		return
	}
	response.OkData(ctx, cc)
	return
}
func GetPwd(operator *emodel.My_user) (username, password string) {
	return fmt.Sprintf("%d", operator.Id), hash_md5_fun(fmt.Sprintf("%d%s%s", operator.Id, operator.Mobile, operator.Email))
}

func (handler *ImHandler) UpdateAvaTarHuanxin(c *gin.Context) {
	operator, err := getOperator(c)
	if err != nil {
		response.Error(c, err)
		return
	}

	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "请上传图片文件"})
		return
	}

	// 打开文件
	openedFile, err := file.Open()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "无法打开文件"})
		return
	}
	defer openedFile.Close()

	// 读取文件内容
	fileBytes, err := ioutil.ReadAll(openedFile)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "读取文件失败"})
		return
	}

	// 检测文件类型
	contentType := http.DetectContentType(fileBytes)
	if contentType != "image/jpeg" && contentType != "image/png" && contentType != "image/gif" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "仅支持JPEG、PNG或GIF图片"})
		return
	}

	// 转换为Base64
	base64Str := base64.StdEncoding.EncodeToString(fileBytes)

	// 构建Data URI
	dataURI := "data:" + contentType + ";base64," + base64Str
	fmt.Println(operator)

	// 返回结果
	c.JSON(http.StatusOK, gin.H{
		"filename":   file.Filename,
		"size":       file.Size,
		"mime_type":  contentType,
		"data_uri":   dataURI,
		"base64_len": len(base64Str),
	})
}
